import com.Users2;
import sun.plugin.dom.core.Element;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

public class Servletcal extends HttpServlet {
    public Servletcal(){
        super();
    }
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("GB2312");
        double f = Double.valueOf(req.getParameter("f"));
        String sym = req.getParameter("symbol");
        double s = Double.valueOf(req.getParameter("s"));
        //HttpSession session = req.getSession();
        Users2 user = new Users2();
        user.setFirst(f);
        user.setSym(sym);
        user.setSecond(s);
        double r=0;
        if(sym.equals("+")){//这里有个疑问，为什么用user.getFirst() , user.getSecond()不行呢？
            r = f+s;
        }else if(sym.equals("-")){
            r = f-s;
        }else if(sym.equals("*")){
            r = f*s;
        }else if(sym.equals("/")){
            r = f/s;
        }
        // 输出计算结果
        //out.println(res);
        req.setAttribute("user", user);
        req.setAttribute("res", r);
        req.getRequestDispatcher("/servletresult.jsp").forward(req, resp);
       // resp.sendRedirect("servletresult.jsp");
    }

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
    public void init() throws ServletException{

    }
}
